% 波的一个神奇现象
% 如果S1参考系观察到一个波在x,y方向上传播，而新的参考系S2相较于参考系S1沿着x轴以速度b运动
% 会发现如果速度b正好=w/kx，就会发现S2中波居然是静止的！
% x上的运动神奇地“抵消”掉了y向的运动！
% Gitee Repo

clc
clear

L = 20;
dx = 0.1;

[x,y] = meshgrid(0:dx:L);
n = size(x,1);
u1 = zeros(n,n);
u2 = zeros(n,n);

t = 0;

T = 5;
v = 1;

w = 2*pi/T;
k = w/v;
theta = 60;
kx = k*cosd(theta);
ky = k*sind(theta);

b = w/kx;

u_func = @(x,y,t) cos(w*t - kx*x - ky*y);

figure
clf

% S1 观察
subplot(1,2,1)
hold on
axis equal
axis ([0,L,0,L,-5,5])
h1 = surf(x,y,u1,'EdgeColor','None');
hx = line([0,0],[0,L/3],[3,3]);
hy = line([0,L/3],[0,0],[3,3]);
title('S1')
xlabel('x'); ylabel('y'); zlabel('u');

% S2观察
subplot(1,2,2)
hold on
axis equal
axis ([0,L,0,L,-5,5])
h2 = surf(x,y,u2,'EdgeColor','None');
title('S2')
xlabel('x'); ylabel('y'); zlabel('u');

for tick = 1:100
    t = 0.1*tick;
    u1 = u_func(x,y,t); %S1 场
    u2 = u_func(x+b*t,y,t); %S2 场

    set(h1,'ZData',u1);
    set(h1,'CData',u1);
    set(h2,'ZData',u2);
    set(h2,'CData',u2);

    % S1 中的 S2 坐标轴
    set(hx,'XData',[b*t, b*t]);
    set(hy,'XData',[b*t, L/2+b*t]);

    drawnow
    pause(0.1)
end
